package com.erpanderp.wam.adm.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.adm.model.entity.Module;
import com.erpanderp.wam.adm.model.entity.Resource;
import com.erpanderp.wam.adm.model.entity.Service;
import com.erpanderp.wam.util.HibernateUtil;

public class ModuleDAO {
	
	public Module createModule(Module module){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.save(module);
		session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
		return module;
		
	}
	public boolean updateModule(Module module){
		boolean moduleUpdated = false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			session.update(module);
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return moduleUpdated;
		
	}

	public List<Module> getAllModules()
	{
		List<Module> modules = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			String queryString = "from Module m";
			Query query = session.createQuery(queryString);
			query.setCacheable(false);
			modules = (List<Module>) query.list();	
			session.close();
			return modules;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	
	public Module getModuleById(Long moduleId)
	{
		Module module = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			module = (Module) session.load(Module.class, moduleId);
			session.getTransaction().commit();
			return module;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	public Module getModule(String moduleName)
	{
		Module module = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.beginTransaction();
			
			String queryString = "from Module m where m.moduleName = :moduleName";
			
			Query query = session.createQuery(queryString).setParameter("moduleName", moduleName);
			query.setCacheable(false);
			List<Module> temp = (List<Module>) query.list();	
			
			session.getTransaction().commit();
			
			if(temp != null && temp.size() > 0){
				return temp.get(0);
			}
			
			return null;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	public static void main(String a[]){
	    //System.out.println(new ModuleDAO().getAllModules().get(0).getModuleName());		
	}

}

